package com.pt5.pthouduan.controller;

import com.pt5.pthouduan.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/passkey")
public class PasskeyController {
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/validate")
    public ResponseEntity<?> validatePasskey(@RequestParam String passkey) {
        boolean isValid = checkPasskeyInDatabase(passkey); // 你的验证逻辑
        if (isValid) {
            System.out.println(passkey);
            return ResponseEntity.ok().build();
        } else {
            System.out.println("budui");
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
    }

    private boolean checkPasskeyInDatabase(String passkey) {
        // 查询数据库或缓存，检查passkey是否有效
        return userMapper.existsByPasskey(passkey);
    }
}